package com.sleep.ruledemo.api;

import com.sleep.ruledemo.domian.DemoEvent;
import com.sleep.ruledemo.domian.R;
import com.sleep.ruledemo.service.DemoService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Optional;

/**
 * @author GuoAo
 * @className DemoService
 * @date 2025/3/20
 * @description TODO
 */
@RestController
@RequestMapping("/demo")
@RequiredArgsConstructor
public class DemoController {
    private final Map<String, DemoService<String,String>> demoServiceMap;

    @PostMapping("/test")
    public R test(@RequestBody DemoEvent event){
        Map<String, String> execute = demoServiceMap.get(event.getType()).execute(event);
        Map<String, String> stringStringMap = Optional.ofNullable(execute).orElse(Map.of("userName", "null"));
        return R.ok(stringStringMap);
    }
}
